angular.module('easego', ['ionic', 
'easego.controllers', 'easego.services','easego.filters','ngCordova'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if (window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
})

.run(function($rootScope,$localstorage,appConfig,UtilService){
  $rootScope.rootUrl = appConfig.rootUrl; //从配置服务中获取服务器后端的Url地址
  $rootScope._ = window._; //在页面里引入lodash

  //在Local Storage里存取登录用户的信息
  $rootScope.currentUserId = $localstorage.get('currentUserId',"");
  if($rootScope.currentUserId) $rootScope.currentUser = 
    $localstorage.getObject('currentUserObj');
  $rootScope.currentUserLogonName = $localstorage.get('currentUserLogonName',"");
  $rootScope.currentUserPassword = $localstorage.get('currentUserPassword',"");

  //已登录用户注销退出
  $rootScope.logout = function(){
    $localstorage.setObject('currentUserObj',null);
    $localstorage.set('currentUserId',"");
    $localstorage.set('currentUserLogonName',"");
    $localstorage.set('currentUserPassword',"");

    $rootScope.currentUser = $localstorage.getObject('currentUserObj');
    $rootScope.currentUserId = $localstorage.get('currentUserId',"");
    $rootScope.currentUserLogonName = "";
    $rootScope.currentUserPassword = "";
  };

  //计算游记的评论数
  $rootScope.CountComments = function(article){
    if(!article) return 0;
    return _.reduce(
      _.map(article.sections, function(section){return section.comments.length;}),
      function(sum,n){return sum+n},
      0);
  };

  //计算游记的点赞数
  $rootScope.CountLikes = function(article){
    if(!article) return 0;
    return _.reduce(
      _.map(article.sections, function(section){return section.likes.length;}),
      function(sum,n){return sum+n},
      0);
  };

  //计算游记经历的天数
  $rootScope.CountDays = function(article){
    if(!article) return 0;
    var dateArray = _.map(article.sections, 
                          function(section){return Date.parse(section.date);});
    return UtilService.dateDiff(_.max(dateArray),_.min(dateArray));
  };
})

.config(function($stateProvider, $urlRouterProvider) {
  $stateProvider

  .state('app', {//根状态
    url: '/app',
    abstract: true,
    templateUrl: 'templates/menu.html',
    controller: 'AppCtrl'
  })

  .state('app.myArticles', {//我的足迹(游记)
    url: '/my-articles',
    views: {
      'menuContent': {
        templateUrl: 'templates/my-articles.html',
        controller: 'MyArticlesCtrl',
      }
    }
  })

  .state('app.articles', {//旅友行踪
    url: '/articles',
    views: {
      'menuContent': {
        templateUrl: 'templates/articles.html',
        controller: 'ArticlesCtrl',
      }
    }
  })
 
  .state('app.articles-show', {//查看整篇游记
    url: '/articles/:articleId',
    views: {
      'menuContent': {
        templateUrl: 'templates/article.html',
        controller: 'ArticleCtrl',
      }
    }
  })

  .state('app.articles-section-show', {//查看游记片段
    url: '/articles/:articleId/:sectionId',
    views: {
      'menuContent': {
        templateUrl: 'templates/section.html',
        controller: 'SectionCtrl',
      }
    }
  })

  .state('app.articles-new', {//新建游记
    url: '/articles/new',
    views: {
      'menuContent': {
        templateUrl: 'templates/article-new.html',
        controller: 'ArticleNewCtrl',
      }
    }
  })

  .state('app.articles-show-author', {//查看整篇游记-作者
    url: '/articles/author/:articleId/:removeBackViewCount',
    views: {
      'menuContent': {
        templateUrl: 'templates/article-author.html',
        controller: 'ArticleAuthorCtrl',
      }
    }
  })

  .state('app.articles-edit-author', {//编辑整篇游记-作者
    url: '/articles/edit-author/:articleId',
    views: {
      'menuContent': {
        templateUrl: 'templates/article-edit-author.html',
        controller: 'ArticleEditAuthorCtrl',
      }
    }
  })

  .state('app.articles-section-show-author', {//查看编辑游记片段-作者
    url: '/articles/author/:articleId/:sectionId/:removeBackViewCount',
    views: {
      'menuContent': {
        templateUrl: 'templates/section-author.html',
        controller: 'SectionAuthorCtrl',
      }
    }
  })

  .state('app.products', {//预约旅行产品列表首页
    url: '/products',
    views: {
      'menuContent': {
        templateUrl: 'templates/products.html',
        controller: 'ProductsCtrl',
      }
    }
  })

  .state('app.productsCategory', {//查看某种类的旅行产品列表
    url: '/products-category/:category',
    views: {
      'menuContent': {
        templateUrl: 'templates/products-category.html',
        controller: 'ProductsCategoryCtrl',
      }
    }
  })

  .state('app.product', {//查看某旅行产品的详情页
    url: '/product/:productId',
    views: {
      'menuContent': {
        templateUrl: 'templates/product.html',
        controller: 'ProductCtrl',
      }
    }
  })

  .state('app.poCreate', {//创建旅行产品订单
    url: '/po-create/:productId',
    views: {
      'menuContent': {
        templateUrl: 'templates/po-create.html',
        controller: 'PoCreateCtrl',
      }
    }
  })

  .state('app.poList', {//当前用户的订单列表
    url: '/po-list',
    views: {
      'menuContent': {
        templateUrl: 'templates/po-list.html',
        controller: 'PoListCtrl',
      }
    }
  })

  .state('app.setting', {//设置导航页
    url: '/setting',
    views: {
      'menuContent': {
        templateUrl: 'templates/setting.html',
        controller: 'SettingCtrl',
      }
    }
  })

  ;
  //默认到旅友行踪页
  $urlRouterProvider.otherwise('/app/articles');
});
